var print_tags = [];
var print_index = 0;
var print_type = 1;
var print_num = 1;
function init_qrcode(lb) {
  $("#print_qrcode").html('<div class="qrcode1" id="qrcode1"></div>');
  var qrcode = new QRCode(document.getElementById("qrcode1"), {
    width: 250,
    height: 250,
    colorDark: "#000000",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.H
  });
  qrcode.makeCode(lb.LabelNum);


}
function init_barcode() {
  JsBarcode("#barcode", "PR-2022-192019013", { height: 100, displayValue: false });
}

function startPrint() {
  var lb = print_tags[print_index];
  $("#print_barcode").html('<svg id="barcode"></svg>');
  $(".mylabel2 .print_ShelfNum").html(lb.JobNum);
  $(".mylabel2 .print_LotNum").html(lb.LotNum);
  $(".mylabel2 .print_PartNum").html(lb.PartNum);
  $(".mylabel2 .print_PackageID").html(lb.PackageID);
  $(".mylabel2 .print_LabelNum").html(lb.LabelNum);
  
  
  JsBarcode("#barcode", lb.LabelNum, { height: 100, displayValue: false });
  setTimeout(function () {
    shareapp2();
  }, 800)

}


function startPrint2() {
  var lb = print_tags[print_index];

  $("#CompanyName").html(userinfo.CompanyName);
  $(".mylabel1 .print_PartNum").html(lb.PartNum);
  $(".mylabel1 .print_JobNum").html(lb.JobNum);
  $(".mylabel1 .print_OrderNum").html(lb.PartDescription);
  $(".mylabel1 .print_TranQty").html(lb.TranQty);
  $(".mylabel1 .print_LotNum").html(lb.LotNum);
  
  init_qrcode(lb);
  $(".mylabel1 .print_LabelNum").html(lb.LabelNum);
  setTimeout(function () {
    shareapp();
  }, 800)

}

function shareapp() {
  $("#mylabel1").attr("class", "mylabel1 mylbshow");
  $("#mylabel1").trigger("click");
  window.pageYOffset = 0;
  document.documentElement.scrollTop = 0
  document.body.scrollTop = 0


  html2canvas(document.querySelector("#mylabel1")).then(function (canvas) {

    var bsimg = convertCanvasToImage(canvas);
    var k = bsimg.replace(/^.*?,/, '');
    var param = {};
    param.bsimg = k;
    $("#mylabel1").attr("class", "mylabel1");
    console.log(k);
    wifiprint(1, k, print_num)
    

  });



}

function shareapp2() {
  $("#mylabel2").attr("class", "mylabel2 mylbshow");
  $("#mylabel2").trigger("click");
  window.pageYOffset = 0;
  document.documentElement.scrollTop = 0
  document.body.scrollTop = 0


  html2canvas(document.querySelector("#mylabel2")).then(function (canvas) {

    var bsimg = convertCanvasToImage(canvas);
    var k = bsimg.replace(/^.*?,/, '');
    var param = {};
    $("#mylabel2").attr("class", "mylabel2");
    param.bsimg = k;
    wifiprint(2, k, print_num)
    console.log(k);

  });



}


function convertCanvasToImage(canvas) {
  var image = new Image();
  image.src = canvas.toDataURL("image/jpg");
  return image.src;
}


function wifiprint(type, imgstr, num) {

  var moduleTsc = api.require('moduleTsc');
  moduleTsc.print({ "type": type, "imgstr": imgstr, "print_num": num }, function (ret, err) {
    if (ret) {
      if (ret.status == 1) {
        var ol = print_tags[print_index];
        print_index++;
        if (!print_tags[print_index]) {
          hideProgress();
          api.alert({
            title: 'Tips',
            msg: lan['Successfully_sent'],
          }, function (ret, err) {

          });
        } else {
          if(print_type == 1){
            startPrint2();
          }else{
            startPrint();
          }
          
        }

      } else {
        hideProgress();
        errMassage(ret.msg)
      }
    }
  });
}

function wificonnect() {

  var param = get_prefs_data("dyparam");
  if (!param) {
    api.toast({
      msg: lan['Printer_Parameters_placeholder'],
      location: 'middle'
    });
    return false;
  }


  var jsfun = 'wificonnect("' + param.ip1 + '",' + param.port1 + ');';
  alert("Confirm to print");
  api.execScript({
      name: 'root',
      script: jsfun
  });
  return;

  var moduleTsc = api.require('moduleTsc');
  moduleTsc.connect({ "ip": param.ip1, "port": parseInt(param.port1) }, function (ret, err) {
    if (ret) {
      if (ret.status == 1) {
        startPrint();
      }
      if (ret.status == 2) {
        hideProgress();
        errMassage(ret.msg)
      }
    }
  });



}


function wificonnect2() {


  var param = get_prefs_data("dyparam");
  if (!param) {
    api.toast({
      msg: lan['Printer_Parameters_placeholder'],
      location: 'middle'
    });
    return false;
  }

  var jsfun = 'wificonnect("' + param.ip2 + '",' + param.port2 + ');';
  alert("Confirm to print");
  api.execScript({
      name: 'root',
      script: jsfun
  });
  return;


  var moduleTsc = api.require('moduleTsc');
  moduleTsc.connect({ "ip": param.ip2, "port": parseInt(param.port2) }, function (ret, err) {
    if (ret) {
      if (ret.status == 1) {
        startPrint2();
      }
      if (ret.status == 2) {
       
        hideProgress();
        errMassage(ret.msg)
      }
    }
  });



}